import MTExternalContent
from pymt import *

class MTPicture(MTExternalContent.MTExternalContent):
    name = 'picture'
    def __init__(self, filename,listPages,kinetics, **kwargs):
        kwargs.setdefault('type', 'picture')        
        super(MTPicture, self).__init__(**kwargs)
        self.listPages=listPages
        self._filename=filename
        self.image=Image(self._filename)
        self.height=200
        self.width = (self.image.width / float(self.image.height)) * self.height
        print self.width
        self.kinetics=kinetics
        self.hold=False
        self.in_move_inside=False
        self.in_zone=False	
        self.in_move_outside=False
        self._zone=None
        self._page=None		

    def on_touch_move(self, touch):	
        if not self.collide_point(*touch.pos):
			return False	
        if(touch.is_held and (touch.xmot == 0 and touch.ymot == 0)):
                        self.hold=True
			self.center=(touch.x,touch.y)			
        if(self.hold==True):
                        print "hold & move"
			for page in self.listPages:
				self._page=page
				media_zones=page._get_media_zones()
				for zone in media_zones:
					pos_zone=self.get_position_zone(zone,page)
					#if the picture is above a zone  
					if(self.in_zone==False and (self.center[0]> pos_zone[0] and self.center[0]< pos_zone[0]+zone.width\
					and self.center[1]> pos_zone[1] and self.center[1]< pos_zone[1]+zone.height)):
						self.in_move_inside=True
						self._zone=zone
						print pos_zone
						return MTExternalContent.MTExternalContent.on_touch_move(self, touch)						
					if(self.in_zone==True):
						pos_zone=self.get_position_zone(self._zone,self._page)
						print pos_zone						
						self.kinetics.add_widget(self)
						self._zone.remove_widget(self)						
						self.pos=(pos_zone[0]-(self.width/2),pos_zone[1]+(self.height/2))
						self.in_move_outside=True
						return False	
					else:					
						self.in_move_inside=False	
						self.in_move_outside=False
			return MTExternalContent.MTExternalContent.on_touch_move(self, touch)
	
	
    def on_touch_up(self, touch):
        
		if (self.in_move_inside==True):
			print "inside"
			self.in_zone=True
			self.in_move_inside=False
			old_width=self.width
			old_height=self.height			
			#resize proportionally
			if (self._zone.width >= self._zone.height):
				self.width=self._zone.width
				self.height=self.height*(self.width/old_width)
				self._zone.height=self.height
			else:
				self.height=self._zone.height
				self.width=self.width*(self.height/old_height)
				self._zone.width=self.width
			#check size
			# if(self.zone.height <= self.zone.height_min):
				# print "height too small"
				# self.zone.height = self.zone.height_min
				# self.zone.width = old_width				
			# elif(self.zone.height >= self.zone.height_max):
				# print "height too big"			
				# self.zone.height = self.zone.height_max
				# self.zone.width = old_width					
			# if(self.zone.width <= self.zone.width_min):
				# print "width too small"			
				# self.zone.width = self.zone.width_min
				# self.zone.height = old_height					
			# elif(self.zone.width >= self.zone.width_max):
				# print "width too big"
				# self.zone.width = self.width_max
				# self.zone.height = old_height			
			self._zone.update_resize_controls()
			self._zone.update_resize_container()
			self._zone.update_resize_buttons()			
			self.pos=(self._zone.width/2,self._zone.height/2)
			rotation = 360 - self.rotation
			MTExternalContent.MTExternalContent.apply_angle_scale_trans(self, rotation, 1., Vector(0, 0), Vector(*self.pos))		
			if (self._zone._get_content()!=None):
				pos_zone=self.get_position_zone(self._zone,self._page)
				self.kinetics.add_widget(self._zone._get_content())
				self._zone._get_content().pos=(pos_zone[0]-(self.width/2),pos_zone[1]+(self.height/2))
				self._zone._get_content().in_zone=False
				self._zone._get_content().in_move_outside=False
				self._zone.remove_widget(self._zone._get_content())				
			self._zone.add_widget(self)
			self.kinetics.remove_widget(self)
			return MTExternalContent.MTExternalContent.on_touch_up(self, touch)
		if (self.in_move_outside ==True):
			print "outside"
			self.in_zone=False
			self.in_move_outside=False
                if not self.collide_point(*touch.pos):
			return MTExternalContent.MTExternalContent.on_touch_up(self, touch)
		self.hold=False			
		return MTExternalContent.MTExternalContent.on_touch_up(self, touch)
		
    def get_position_zone(self, zone, page):
		x=(zone.x-(zone.width/2))+(page.x-(page.width/2))
		y=(zone.y-(zone.height/2))+(page.y-(page.height/2))
		pos=(x,y)
		return pos

    def draw(self):
        super(MTPicture, self).draw()
        if self.image:
            self.image.size = self.size
            self.image.draw()
	
    def _get_filename(self):
        return self._filename
    id = property(_get_filename) 	
